package com.example.web.datasource;

/**
 * @program: springboot_course
 * @description: 用于保存本地数据源
 * @author: ChenZhiXiang
 * @create: 2019-06-12 09:24
 **/
public class DataSourceContextHolder {
    /**
     * 默认数据源
     */
    public static final String DEFAULT_DS = "primaryDataSource";
    /**
     * ThreadLocal之后会进行讲解
     */
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    /** 设置数据源名 */
    public static void setDB(String dbType) {
        System.out.println("切换到{"+dbType+"}数据源");
        contextHolder.set(dbType);
    }

    /** 获取数据源名 */
    public static String getDB() {
        return (contextHolder.get());
    }

    /** 清除数据源名 */
    public static void clearDB() {
        contextHolder.remove();
    }
}
